﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using FCTC_Web.Models;
using FCTC_Web.DAL;
using DAL;
using System.IO;
using System.Drawing;
using FCTC_Web.Utilities;

namespace FCTC_Web.Controllers.Maintains
{
    [HandleError]
    public partial class ImageController : Controller
    {
        private void Claim(string relativeType, int? i)
        {
            ViewBag.Claim = "For" + relativeType;
            ViewBag.ClaimId = i;
            ViewBag.ClaimPage = ViewBag.Claim + "?" + "relativeId=" + i.ToString();
        }

        #region Article

        public ViewResult IndexForArticle(int relativeId, int? pageId)
        {
            Claim("Article", (int)relativeId);
            return View("Index", Pager6.Ins.GetPage<ImageModel>(relativeId, pageId, "ArticleID"));
        }

        public ViewResult CreateForArticle(int relativeId)
        {
            Claim("Article", relativeId);
            return View("Create");
        }

        [HttpPost]
        public ActionResult CreateForArticle(ImageModel image, int? relativeId)
        {
            Claim("Article", relativeId);
            image.ArticleID = relativeId;
            var img = UploadHelper.UploadImg(Request, image);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "ArticleID");
            return RedirectToAction("IndexForArticle", new { relativeId = relativeId, pageId = pageId });
        }
        public ViewResult EditForArticle(int? id)
        {
            var data = CH.GetDataById<ImageModel>(id);
            Claim("Article", (int)data.ArticleID);
            return View("Edit", data);
        }

        [HttpPost]
        public ActionResult EditForArticle(ImageModel image, int? relativeId)
        {
            Claim("Article", relativeId);
            if (ModelState.IsValid)
            {
                image.ArticleID = relativeId;
                var img = UploadHelper.UploadImg(Request, image);
                var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "ArticleID");
                return RedirectToAction("IndexForArticle", new { relativeId = relativeId, pageId = pageId });
            }
            return View("Edit", image);
        }

        public ActionResult DeleteForArticle(int? id)
        {
            ImageModel image = CH.GetDataById<ImageModel>(id);
            Claim("Article", image.ArticleID);
            return View("Delete", image);
        }

        [HttpPost, ActionName("DeleteForArticle")]
        public ActionResult DeleteConfirmedForArticle(int id, int? relativeId)
        {
            var img = CH.GetDataById<ImageModel>(id);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "ArticleID");
            try
            {
                Claim("Article", img.ArticleID);
                img.ArticleID = null;
                CH.Edit<ImageModel>(img);
                CH.Delete<ImageModel>(id);
            }
            catch (DataException e)
            {
            }
            return RedirectToAction("IndexForArticle", new { relativeId = relativeId, pageId = pageId });
        }
        #endregion

        #region Vehicle

        public ViewResult IndexForVehicle(int relativeId, int? pageId)
        {
            Claim("Vehicle", (int)relativeId);
            return View("Index", Pager6.Ins.GetPage<ImageModel>(relativeId, pageId, "VehicleID"));
        }

        public ViewResult CreateForVehicle(int relativeId)
        {
            Claim("Vehicle", relativeId);
            return View("Create");
        }

        [HttpPost]
        public ActionResult CreateForVehicle(ImageModel image, int? relativeId)
        {
            Claim("Vehicle", relativeId);
            image.VehicleID = relativeId;
            var img = UploadHelper.UploadImg(Request, image);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "VehicleID");
            return RedirectToAction("IndexForVehicle", new { relativeId = relativeId, pageId = pageId });
        }
        public ViewResult EditForVehicle(int? id)
        {
            var data = CH.GetDataById<ImageModel>(id);
            Claim("Vehicle", (int)data.VehicleID);
            return View("Edit", data);
        }

        [HttpPost]
        public ActionResult EditForVehicle(ImageModel image, int? relativeId)
        {
            Claim("Vehicle", relativeId);
            if (ModelState.IsValid)
            {
                image.VehicleID = relativeId;
                var img = UploadHelper.UploadImg(Request, image);
                var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "VehicleID");
                return RedirectToAction("IndexForVehicle", new { relativeId = relativeId, pageId = pageId });
            }
            return View("Edit", image);
        }

        public ActionResult DeleteForVehicle(int? id)
        {
            ImageModel image = CH.GetDataById<ImageModel>(id);
            Claim("Vehicle", image.VehicleID);
            return View("Delete", image);
        }

        [HttpPost, ActionName("DeleteForVehicle")]
        public ActionResult DeleteConfirmedForVehicle(int id, int? relativeId)
        {
            var img = CH.GetDataById<ImageModel>(id);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "VehicleID");
            try
            {
                Claim("Vehicle", img.VehicleID);
                img.VehicleID = null;
                CH.Edit<ImageModel>(img);
                CH.Delete<ImageModel>(id);
            }
            catch (DataException e)
            {
            }
            return RedirectToAction("IndexForVehicle", new { relativeId = relativeId, pageId = pageId });
        }
        #endregion

        #region Shop
        public void ClaimShop()
        {
            ViewBag.Claim = "ForShop";
        }

        public ViewResult IndexForShop(int relativeId, int? pageId)
        {
            Claim("Shop", (int)relativeId);
            return View("Index", Pager6.Ins.GetPage<ImageModel>(relativeId, pageId, "ShopID"));
        }

        public ViewResult CreateForShop(int relativeId)
        {
            Claim("Shop", relativeId);
            return View("Create");
        }

        [HttpPost]
        public ActionResult CreateForShop(ImageModel image, int? relativeId)
        {
            Claim("Shop", relativeId);
            image.ShopID = relativeId;
            var img = UploadHelper.UploadImg(Request, image);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "ShopID");
            return RedirectToAction("IndexForShop", new { relativeId = relativeId, pageId = pageId });
        }
        public ViewResult EditForShop(int? id)
        {
            var data = CH.GetDataById<ImageModel>(id);
            Claim("Shop", (int)data.ShopID);
            return View("Edit", data);
        }

        [HttpPost]
        public ActionResult EditForShop(ImageModel image, int? relativeId)
        {
            Claim("Shop", relativeId);
            if (ModelState.IsValid)
            {
                image.ShopID = relativeId;
                var img = UploadHelper.UploadImg(Request, image);
                var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "ShopID");
                return RedirectToAction("IndexForShop", new { relativeId = relativeId, pageId = pageId });
            }
            return View("Edit", image);
        }

        public ActionResult DeleteForShop(int? id)
        {
            ImageModel image = CH.GetDataById<ImageModel>(id);
            Claim("Shop", image.ShopID);
            return View("Delete", image);
        }

        [HttpPost, ActionName("DeleteForShop")]
        public ActionResult DeleteConfirmedForShop(int id, int? relativeId)
        {
            var img = CH.GetDataById<ImageModel>(id);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "ShopID");
            try
            {
                Claim("Shop", img.ShopID);
                img.ShopID = null;
                CH.Edit<ImageModel>(img);
                CH.Delete<ImageModel>(id);
            }
            catch (DataException e)
            {
            }
            return RedirectToAction("IndexForShop", new { relativeId = relativeId, pageId = pageId });
        }
        #endregion

        #region Estimator
        
        public ViewResult IndexForEstimator(int relativeId,  int? pageId)
        {
            Claim("Estimator",(int)relativeId);
            return View("Index", Pager6.Ins.GetPage<ImageModel>(relativeId,pageId,"EstimatorID"));
        }

        public ViewResult CreateForEstimator(int relativeId)
        {
            Claim("Estimator", relativeId);
            return View("Create");
        }

        [HttpPost]
        public ActionResult CreateForEstimator(ImageModel image, int? relativeId)
        {
            Claim("Estimator", relativeId);
            image.EstimatorID = relativeId;
            var img = UploadHelper.UploadImg(Request, image);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "EstimatorID");
            return RedirectToAction("IndexForEstimator", new { relativeId = relativeId, pageId = pageId });
        }

        public ViewResult EditForEstimator(int? id)
        {
             var data = CH.GetDataById<ImageModel>(id);
             Claim("Estimator", (int)data.EstimatorID);
             return View("Edit", data);
        }

        [HttpPost]
        public ActionResult EditForEstimator(ImageModel image, int? relativeId)
        {
            Claim("Estimator", relativeId);
            if (ModelState.IsValid)
            {
                image.EstimatorID = relativeId;
                var img = UploadHelper.UploadImg(Request, image);
                var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "EstimatorID");
                return RedirectToAction("IndexForEstimator", new { relativeId = relativeId, pageId = pageId });
            }
            return View("Edit",image);
        }

        public ActionResult DeleteForEstimator(int? id)
        {
          
            ImageModel image = CH.GetDataById<ImageModel>(id);
            Claim("Estimator", image.EstimatorID);
            return View("Delete",image);
        }

        [HttpPost, ActionName("DeleteForEstimator")]
        public ActionResult DeleteConfirmedForEstimator(int id, int? relativeId)
        {
            var img = CH.GetDataById<ImageModel>(id);
            var pageId = Pager6.Ins.GetPageId<ImageModel>(relativeId, img.ID, "EstimatorID");
            try
            {
                Claim("Estimator", img.EstimatorID);
                img.EstimatorID = null;
                CH.Edit<ImageModel>(img);
                CH.Delete<ImageModel>(id);
            }
            catch (DataException e)
            {
            }
            return RedirectToAction("IndexForEstimator", new { relativeId = relativeId, pageId = pageId });
        }

        #endregion

        public ViewResult Index(int? pageId)
        {
            ///  Claim();
            return View("Index", Pager6.Ins.GetPage<ImageModel>(pageId));
        }

        //public ViewResult Indexccc()
        //{
        //    ///  Claim();
        //    return View("Index");
        //}

        public ViewResult Details(int? id)
        {
            ImageModel image = CH.GetDataById<ImageModel>(id);
            return View(image);
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(ImageModel image)
        {

            if (ModelState.IsValid)
            {
                var img = UploadHelper.UploadImg(Request, image);
                var pageId = Pager6.Ins.GetPageId<ImageModel>(img.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            return View(image);
        }

        public ActionResult Edit(int? id)
        {
            return View(CH.GetDataById<ImageModel>(id));
        }

        [HttpPost]
        public ActionResult Edit(ImageModel image)
        {
            if (ModelState.IsValid)
            {
                var img = UploadHelper.UploadImg(Request,image);
                CH.Edit<ImageModel>(img);
             //   return JavaScript("window.close();");

                var pageId = Pager6.Ins.GetPageId<ImageModel>(img.ID);
                return RedirectToAction("Index", new { pageId = pageId });
            }
            return View(image);
        }

        public ActionResult ShowImage(int? id)
        {
            var image = CH.GetDataById<ImageModel>(id);
            if (image != null)
            {
                byte[] imageData = image.ImageData;
                if (imageData == null) return null;
                return File(imageData, image.ContentType);
            }
            return null;
        }

        public ActionResult Delete(int? id)
        {
            ImageModel image = CH.GetDataById<ImageModel>(id);
            return View(image);
        }

        /// <summary>
        /// 删除时需要编辑引用，避免报异常
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            try
            {
                var img= CH.GetDataById<ImageModel>(id);
                img.EstimatorID = null;
                img.ShopID = null;
                img.VehicleID = null;
                CH.Edit<ImageModel>(img);
                CH.Delete<ImageModel>(id);
            }
            catch (DataException e)
            { 
                //need handle exception that the image has been reference
            }
            var pageId = Pager6.Ins.GetPageId<ImageModel>(id);
            return RedirectToAction("Index", new { pageId = pageId });
        }

        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }
}